package com.xiaoran.service.connector.handler;

import com.xiaoran.common.data.RpcResponse;
import com.xiaoran.common.utils.JsonSerializerUtil;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

public class RpcServerEncodeHandler extends MessageToByteEncoder<RpcResponse> {
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, 
                          RpcResponse rpcResponse, 
                          ByteBuf byteBuf) throws Exception {
        byte[] bytes = JsonSerializerUtil.serialize(rpcResponse);
        int length = bytes.length;
        byteBuf.writeInt(length);
        byteBuf.writeBytes(bytes);
    }
}
